package com.project.lk.nurseapp.utils;

import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.PopupWindow;

/**
 * 功能介绍：
 * 项目名：
 * 模块名称：
 * 作者：LK
 * 日期：2018/1/9
 * 修改日期：
 */
public class PopWindowManager {



    //显示弹窗
    public static void  showAsDropWindow(View view, final View contentView, final PopupWindow  popupWindow,
                                           final int res){



        //设置弹窗的项目点击事件
        popupWindow.setTouchable(true);
        // view添加OnTouchListener监听判断获取触屏位置如果在布局外面则销毁弹出框
        contentView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                // getTop View自身的顶边到其父布局顶边的距离,因为是根目录所以为0
                int height = contentView.findViewById(res).getTop();
                // getY 点击事件距离控件顶边的举例
                int y = (int) event.getY();
                // 当抬起 并且 y>height时（也就是 只要不点击ListView范围内）,dismiss popupWindow
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    if (y > height) {
                        popupWindow.dismiss();
                    }
                }
                return true;
            }
        });

        // 设置SelectPicPopupWindow弹出窗体的背景
        popupWindow.setBackgroundDrawable(new ColorDrawable(0xb0000000));
        // 如果不设置PopupWindow的背景，无论是点击外部区域还是Back键都无法dismiss弹框

        // 设置好参数之后再show
        popupWindow.showAsDropDown(view);
        //popupWindow.showAtLocation(view, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);

    }


    //显示弹窗
    public static void  showAsLocationWindow(View view, final View contentView, final PopupWindow  popupWindow,
                                         final int res){



        //设置弹窗的项目点击事件
     popupWindow.setTouchable(true);
       // popupWindow.setOutsideTouchable(false);
        // view添加OnTouchListener监听判断获取触屏位置如果在布局外面则销毁弹出框
        contentView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                // getTop View自身的顶边到其父布局顶边的距离,因为是根目录所以为0
                int height = contentView.findViewById(res).getTop();
                // getY 点击事件距离控件顶边的举例
                int y = (int) event.getY();
                // 当抬起 并且 y>height时（也就是 只要不点击ListView范围内）,dismiss popupWindow
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    if (y> height) {
                       //popupWindow.dismiss();
                    }
                }
                return true;
            }
        });

        // 设置SelectPicPopupWindow弹出窗体的背景
        popupWindow.setBackgroundDrawable(new ColorDrawable(0xb0000000));
        // 如果不设置PopupWindow的背景，无论是点击外部区域还是Back键都无法dismiss弹框

        // 设置好参数之后再show
     //   popupWindow.showAsDropDown(view);
        popupWindow.showAtLocation(view, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);

    }

    //显示弹窗
    public static void  showFitAsLocationWindow(View view, final View contentView, final PopupWindow  popupWindow,
                                             final int res,int y){



        //设置弹窗的项目点击事件
        popupWindow.setTouchable(true);
        // popupWindow.setOutsideTouchable(false);
        // view添加OnTouchListener监听判断获取触屏位置如果在布局外面则销毁弹出框
        contentView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                // getTop View自身的顶边到其父布局顶边的距离,因为是根目录所以为0
                int height = contentView.findViewById(res).getTop();
                // getY 点击事件距离控件顶边的举例
                int y = (int) event.getY();
                // 当抬起 并且 y>height时（也就是 只要不点击ListView范围内）,dismiss popupWindow
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    if (y> height) {
                        //popupWindow.dismiss();
                    }
                }
                return true;
            }
        });

        // 设置SelectPicPopupWindow弹出窗体的背景
        popupWindow.setBackgroundDrawable(new ColorDrawable(0xb0000000));
        // 如果不设置PopupWindow的背景，无论是点击外部区域还是Back键都无法dismiss弹框

        // 设置好参数之后再show
        //   popupWindow.showAsDropDown(view);
        popupWindow.showAtLocation(view,Gravity.NO_GRAVITY, 0, y + view.getHeight());

    }
}
